const os = @import("../os.zig");
const driver = @import("../driver.zig");

comptime {
    driver.register(&init, "sifive,test1");
}

var syscon: *volatile u32 = undefined;

fn init(arg: ?*const anyopaque) void {
    const node: *const os.fdt.Node = @ptrCast(@alignCast(arg.?));
    const base = os.fdt.base(node);
    syscon = @ptrFromInt(base);
}

pub const ShutdownStatus = enum(u16) {
    fail = 0x3333,
    pass = 0x5555,
    reset = 0x7777,
};

// https://github.com/qemu/qemu/blob/56c6e249b6988c1b6edc2dd34ebb0f1e570a1365/hw/misc/sifive_test.c#L35-L59
pub fn shutdown(status: ShutdownStatus, code: u16) noreturn {
    const value: u32 = @as(u32, code) << 16;
    syscon.* = value + @intFromEnum(status);
    unreachable;
}
